﻿using Meiam.System.Hostd.Authorization;
using Meiam.System.Interfaces;
using Meiam.System.Model;
using Meiam.System.Model.Dto;
using Meiam.System.Model.Dto.Business;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Meiam.System.Hostd.Controllers.Business
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EmployeeController : BaseController
    {
        /// <summary>
        /// 日志管理接口
        /// </summary>
        private readonly ILogger<EmployeeController> _logger;

        private readonly IEmployeeProductService _employeeProductService;

        public EmployeeController(ILogger<EmployeeController> logger, IEmployeeProductService employeeProductService)
        {
            _logger = logger;
            _employeeProductService = employeeProductService;
        }

        [HttpPost]
        public IActionResult QueryEmployeeProduct([FromBody] EmployeeProductDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<EmployeeProduct>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.QueryText), m => m.employeeid.Contains(parm.QueryText));

            var response = _employeeProductService.GetPages(predicate.ToExpression(), parm);

            return toResponse(response);
        }

        [HttpPost]
        public IActionResult UpdateEmployeeProduct([FromBody] EmployeeProductUpdateDto parm)
        {
            if (!_employeeProductService.Any(m => m.employeeid == parm.employeeid || m.name == parm.name))
            {
                return toResponse(StatusCodeType.Error, $"工号 {parm.employeeid} 不存在！");
            }

            try
            {
                string sql = string.Format("{0}{1}={2} where employeeid='{3}' and spec='{4}' and datatype='{5}'", "update employee_product set ", parm.day, parm.num, parm.employeeid, parm.spec, parm.datatype);

                var response = _employeeProductService.GetOneRowOneColumn(sql);

                return toResponse(response);
            }
            catch (Exception)
            {
                throw;
            }
     
        }

        //[HttpPost]
        //[Authorization(Power = "PRIV_EMPLOYEEPRODUCT_UPDATE")]
        //public IActionResult Update([FromBody] CompanyUpdateDto parm)
        //{

        //}
    }
}
